import {getStatusesList, submittransferstatus, taxStatusDetail, submitpresentstatus, exportTaxStatusList} from '../../services/drawback/taxStatuses';
import {exportInvoice} from '../../services/drawback/invoice';
import {parse} from 'qs';
import { message } from 'antd';
import Constants from '../../utils/Constants';
import {downloadFile} from '../../utils/commons'

const dateFormat = 'YYYY-MM-DD';

export default {
    namespace: 'taxStatuses',
    state: {
        search: { // 查询条件
            orderno: '',
            contractNo:'',
            declaratNo: '',
            drawbackFianceStatus: '',
            presentStatus: '',
            transferAccountStatus: '',
            exportStart: '',
            exportEnd: '',
            cstName: ''
        },
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list: [],
        editModel: { // 编辑视图
            defaultWidth: 1000, // 缺省宽度800
            width: 1000, // 宽度
            showStatus: false, // 默认不显示
            confirmLoading: false, // 确认按钮loading状态
            title: '退税状态', // 标题
            form: {

            }
        }
    },
    reducers: {

        /**
         * 设置查询条件
         *
         * @param state
         * @param actio
         * @returns {{search}}
         */
        setSearch(state, action) {
            return {...state, search: action.payload.search};
        },

        /**
         * 是否显示
         *
         * @param state
         * @param action
         * @returns {{showStatus: (boolean|*)}}
         */
        setEditModel(state, action) {
            let payload = action.payload;

            let newState = {...state.editModel};
            if (payload.showStatus != undefined) {
                newState.showStatus = payload.showStatus;
            }
            if(payload.width != undefined) {
                newState.width = payload.width;
            }
            if (payload.title != undefined) {
                newState.title = payload.title;
            }
            if (payload.confirmLoading != undefined) {
                newState.confirmLoading = payload.confirmLoading;
            }

            if(payload.type != undefined) {
                newState.type = payload.type;
            }
            if(payload.form != undefined) {
                newState.form = payload.form;
            }

            if(payload.orderId != undefined) {
                newState.orderId = payload.orderId;
            }

            if(payload.step != undefined) {
                newState.step = payload.step;
            }

            if(payload.width != undefined) {
                newState.width = payload.width;
            }

            if(payload.form != undefined) {
                newState.form = payload.form;
            }

            return {...state, editModel: {...newState}};
        },

        /**
         * 设置数据列表
         *
         * @param state
         * @param action
         */
        setList(state, { payload }) {
            return {...state, list: payload.list, page: payload.page}
        },

        /**
         * 设置页码
         *
         * @param state
         * @param payload
         * @returns {{page: *}}
         */
        setPage(state, { payload }) {
            return {...state, page: payload.page}
        }
    },
    effects: {

        /**
         * 分页获取退税列表
         * @param call
         * @param put
         * @param select
         */
        *getStatusesList({}, {call, put, select}) {
            const search = yield select(state => state.taxStatuses.search);
            const page = yield select(state => state.taxStatuses.page);

            if (search.time != null && search.time != undefined) {
                if(search.time.length >=2 ){
                    if(search.time[0] != ''){
                        search.exportStart = search.time[0].format(dateFormat);
                    }
                    if(search.time[1] != ''){
                        search.exportEnd = search.time[1].format(dateFormat);
                    }

                }
            }


            const res = yield call(getStatusesList, {
                orderNo: search.orderno,
                contractNo: search.contractNo,
                declaratNo: search.declaratNo,
                drawbackFianceStatus: search.drawbackFianceStatus,
                cstName: search.cstName,
                presentStatus: search.presentStatus,
                transferAccountStatus: search.transferAccountStatus,
                exportStart: search.exportStart,
                exportEnd: search.exportEnd,
                page
            });


            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                yield put({
                    type: 'setList',
                    payload: {
                        list: content.list,
                        page: content.page == undefined ? Constants.DEFAULTPAGE  : content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 修改退税状态
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *submittransferstatus({ payload }, { call, put, select }) {
            let presentstatus = payload.presentstatus;

            //交单
            if(presentstatus != 1){
                const res = yield call(submitpresentstatus, payload);
                if (res.data.resultCode === "ok") {
                    message.info("交单成功！");
                    yield put({
                        type: 'setEditModel',
                        payload: {
                            showStatus: false
                        }
                    });
                    yield put({
                        type: 'getStatusesList'
                    });
                } else {
                    message.error(res.data.errMessage);
                }
            }else{
                const res = yield call(submittransferstatus, payload);
                if (res.data.resultCode === "ok") {
                    message.info("提交成功！");
                    yield put({
                        type: 'setEditModel',
                        payload: {
                            showStatus: false
                        }
                    });
                    yield put({
                        type: 'getStatusesList'
                    });
                } else {
                    message.error(res.data.errMessage);
                }
            }

        },


        /**
         * 获取退税详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getTaxStatusDetailById({ payload }, { call, put, select }) {
            const res = yield call(taxStatusDetail, { orderId: payload.orderId});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: true,
                        confirmLoading: false,
                        title: "退税状态",
                        type: 'update',
                        width : 700,
                        form: {
                            id:  content.id,
                            orderno: content.orderno,
                            filename: content.filename,
                            presenttime: content.presenttime,
                            sendreceiptpage: content.sendreceiptpage,
                            inputboxpage: content.inputboxpage,
                            url: content.url,
                            transferaccountstatus: content.transferaccountstatus,
                            presentstatus: content.presentstatus,

                        }
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        //导出开票资料
        *exportInvoice({payload}, {call, put, select}) {
            const res = yield call(exportInvoice, {orderId: payload.orderId});
        },


        *downloadFile({payload}, {call, put, select}) {
            let type = payload.type;
            let url = payload.url;
            downloadFile(type, url);
        },

        /**
         * 导出退税状态列表
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {IterableIterator<*>}
         */
        *exportTaxStatusList({payload}, {call, put, select}) {
            const res = yield call(exportTaxStatusList, {
                orderNo: payload.orderNo,
                declaratNo: payload.declaratNo,
                drawbackFianceStatus: payload.drawbackFianceStatus,
                cstName: payload.cstName,
                presentStatus: payload.presentStatus,
                transferAccountStatus: payload.transferAccountStatus,
                exportStart: payload.exportStart,
                exportEnd: payload.exportEnd,
                contractNo: payload.contractNo,

            });
        },
    },
    subscriptions: {
        setup({history, dispatch}) {
            return history.listen(({pathname}) => {
                if(pathname.includes('drawback/taxStatuses')) {
                    dispatch({
                        type: 'getStatusesList'
                    });
                }

            });
        }
    }

};
